$(function(){
	var tableSource = new $.jqx.dataAdapter({
		dataType: "json",
		dataFields:[
		    { name: "id", type: "int" },
		    { name: "name", type: "string" },
		    { name: "logo", type: "string"},
		    { name: "url", type: "string"},
		    { name: "isrecommend", type: "int"},
		    { name: "recommend_start_time", type: "date"},
		    { name: "recommend_end_time", type: "date"}
		],
		id: "id",
		url: "brand/all.do"
	});
	$("#recommend").click(function(){
		var selected = getSelected();
		if(!selected){
			return false;
		}
		editingIndex = selected.index;
		editingData = selected.row;
	 	_editerIndex = layer.open({
			id: "brandRecommend",
			title: "设置品牌推荐",
		    type: 1,
		    skin: "layui-layer-rim",
		    area: ["720px", "500px"],
		    content: 
		    '<div id="brand-recommend" class="form" style="height: 100%;overflow: auto;">' +
				'<input type="hidden" name="id" />' +
				'<input type="hidden" name="submit-url" value="brand/recommend.do" />' +
				'<label class="from-label" for="name">品牌名称：</label>' +
				'<input type="text" name="name" allowBlank="false" class="form-control" placeholder="品牌名称" readonly="true" />' +
				'<div class="input-box"><label class="from-label" for="recommend_start_time">开始时间:</label>' +
				'<div name="recommend_start_time" allowBlank="false" placeholder="开始时间" id="recommend_start_time" style="display: inline-block;margin:10px 0;vertical-align:middle"></div></div>' +
				'<div class="input-box"><label class="from-label" for="recommend_end_time">结束时间:</label>' +
				'<div name="recommend_end_time" allowBlank="false" placeholder="结束时间" id="recommend_end_time" style="display: inline-block;margin:10px 0;vertical-align:middle"></div></div>' +
				'<br>' +
			'</div>' +
			'<div class="form" style="position: absolute;bottom: 0px;width: 100%;text-align: right;background-color: #DDD;">' +
				'<input type="button" class="btn btn-sm btn-success" onclick="submitEditer(this)" value="保存"/>' +
				'<input type="button" class="btn btn-sm btn-info" onclick="closeEditer(this)" value="返回"/>' +
			'</div>',
		});
		$("#recommend_start_time").jqxDateTimeInput({ width: "200px", height: "34px", formatString: "yyyy-MM-dd", value: null });
		$("#recommend_end_time").jqxDateTimeInput({ width: "200px", height: "34px", formatString: "yyyy-MM-dd", value: null });
	 	var displayData = clone(editingData);
	 	delete displayData.recommend_start_time;
	 	delete displayData.recommend_end_time;
	 	$("input[name='id']").val(editingData['id']);
	 	setData($("#brand-recommend"), displayData);
	});
	$("#dataTable").jqxDataTable({
		width: "100%",
        source: tableSource,
        pageable: true,
        pageSize: 15,
        serverProcessing: true,
        pagerButtonsCount: 10,
        columnsResize: true,
        sortable: true,
        columns: [
            { text: "序号", width: 50, cellsRenderer: function(row) { return (row + 1).toString(); } },
            { text: "品牌名称", dataField: "name" },
            { text: "访问链接", dataField: "url" },
            { text: "是否推荐", dataField: "isrecommend", 
            	cellsRenderer: function(row, column, value, rowData){
            		return ["否","是"][value];
            	}
            },
            { text: "开始推荐", dataField: "recommend_start_time",
            	cellsRenderer: function(row, column, value, rowData) {
            		return rowData.isrecommend ? format(value, "Y-M-D H:I") : " - - ";
            	}
            },
            { text: "结束推荐", dataField: "recommend_end_time",
            	cellsRenderer: function(row, column, value, rowData) {
            		return rowData.isrecommend ? format(value, "Y-M-D H:I") : " - - ";
            	}
            },
            { dataField: "logo", hidden: true }
        ],
	});
	$("#dataTable").on("rowDoubleClick",function(info, args){
		if(args && args.flag){
			info = args;
		}
		editingIndex = info.args.index;
		editingData = info.args.row;
	 	_editerIndex = layer.open({
			id: "brandEditer",
			title: "添加/修改/查看品牌信息",
		    type: 1,
		    skin: "layui-layer-rim",
		    area: ["720px", "500px"],
		    content: 
		    '<form id="brand-editer" class="form" method="post" enctype="multipart/form-data" style="height: 100%;overflow: auto;">' +
				'<input type="hidden" name="id" />' +
				'<input type="hidden" name="submit-url" value="brand/update.do" />' +
				'<div class="input-box">' +
				'<label class="from-label update" for="name">品牌名称：</label>' +
				'<input type="text" name="name" allowBlank="false" class="form-control" placeholder="品牌名称" />' +
				'</div>' +
				'<br><div class="input-box">' +
				'<label class="from-label update" for="name">访问链接：</label>' +
				'<input type="text" name="url" allowBlank="false" class="form-control" placeholder="访问链接,必须加上http:// 例：http://www.t10mall.com" />' +
				'</div>' +
				'<br><label class="from-label" for="photo">LOGO：</label>' +
				'<div class="fileupload fileupload-new data-provides" style="display:inline-block;vertical-align: middle;">' +
					'<div class="form-control fileupload-new thumbnail" style="width:200px;height:200px;padding:0;line-height:200px;">' +
						'<img id="logo" name="logo" />' +
					'</div>' +
					'<div class="form-control fileupload-preview fileupload-exists thumbnail" style="width: 200px; height: 150px;"></div>' +
					'<div id="uploadimg">' +
						'<input type="file" name="photo" id="photo" style="display:none;" />' +
						'<label for="photo" class="fileupload-new btn btn-sm btn-success" style="margin:0">选择文件</label>' +
						'<label for="photo" class="fileupload-exists btn btn-sm btn-success" style="margin:0">更改</label>' +
						'<span  class="fileupload-exists btn btn-sm btn-danger" data-dismiss="fileupload" onclick="removePicture(this)">移除</span>' +
					'</div>' +
				'</div>' +
			'</form>' +
			'<div class="form" style="position: absolute;bottom: 0px;width: 100%;text-align: right;background-color: #DDD;">' +
				'<input type="button" class="btn btn-sm btn-success" onclick="submitEditer(this)" value="保存"/>' +
				'<input type="button" class="btn btn-sm btn-info" onclick="closeEditer(this)" value="返回"/>' +
			'</div>',
		});
	 	$("input[name='id']").val(editingData['id']);
	 	setData($("#brand-editer"), editingData);
	});
});

function removePicture() {
	$(".fileupload").fileupload("clear");
}